package com.dingreading.cloud.admin.util.bean;

import com.dingreading.cloud.admin.dto.StockOutDeliveryDto;
import com.dingreading.cloud.admin.entity.*;
import com.dingreading.cloud.common.util.DateUtil;
import com.dingreading.cloud.common.util.DoubleUtil;
import com.dingreading.cloud.common.util.KitUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.List;

public class PurchaseUtil {

    /**
     * 新建入库记录
     */
    public static WhStockIn newStockIn(String headStaffUid, String headStaffName, WhPurchaseGoods goods, Integer realNum, String remark) {
        WhStockIn data = new WhStockIn();
        data.setInCode(KitUtil.getInCode());
        data.setInType(1);
        data.setPurchaseCode(goods.getPurchaseCode());
        data.setPurchaseGoodsId(goods.getId());
        data.setSupplierId(goods.getSupplierId());
        data.setSupplierName(goods.getSupplierName());
        data.setGoodsId(goods.getGoodsId());
        data.setGoodsName(goods.getGoodsName());
        data.setGoodsTypeId(goods.getGoodsTypeId());
        data.setGoodsTypeName(goods.getGoodsTypeName());
        data.setRealStatus(goods.getRealStatus());
        data.setUnit(goods.getUnit());
        data.setUnitPrice(goods.getUnitPrice());
        data.setSupplierPrice(goods.getSupplierPrice());
        data.setBuyNum(goods.getBuyNum());
        data.setRealNum(realNum);
        data.setCreator(headStaffName);
        data.setCreatorUid(headStaffUid);
        data.setRemark(remark);
        data.setUsableNum(0);
        data.setStatus(2);
        data.setStatusName("入库中");
        return data;
    }

    /**
     * 新建入库记录，并入库完成
     */
    public static WhStockIn newStockInOk(String headStaffUid, String headStaffName, WhPurchaseGoods goods, Integer realNum, String remark, Date time) {
        WhStockIn data = new WhStockIn();
        data.setInCode(KitUtil.getInCode());
        data.setInType(1);
        data.setPurchaseCode(goods.getPurchaseCode());
        data.setPurchaseGoodsId(goods.getId());
        data.setSupplierId(goods.getSupplierId());
        data.setSupplierName(goods.getSupplierName());
        data.setGoodsId(goods.getGoodsId());
        data.setGoodsName(goods.getGoodsName());
        data.setGoodsTypeId(goods.getGoodsTypeId());
        data.setGoodsTypeName(goods.getGoodsTypeName());
        data.setRealStatus(goods.getRealStatus());
        data.setUnit(goods.getUnit());
        data.setUnitPrice(goods.getUnitPrice());
        data.setSupplierPrice(goods.getSupplierPrice());
        data.setBuyNum(goods.getBuyNum());
        data.setRealNum(realNum);
        data.setUsableNum(realNum);
        data.setInAmount(goods.getPrice());
        data.setCreator(headStaffName);
        data.setCreatorUid(headStaffUid);
        data.setOperator(headStaffName);
        data.setOperatorUid(headStaffUid);
        data.setRemark(remark);
        data.setStatus(1);
        data.setStatusName("入库完成");
        data.setOkTime(time);
        return data;
    }

    /**
     * 出库退回，新建入库记录并入库完成
     */
    public static WhStockIn returnNewStockIn(String headStaffUid, String headStaffName, WhStockIn stockIn, Long outGoodsId,
                                             Integer realNum, Double wetAvgPrice, Date time) {
        WhStockIn data = new WhStockIn();
        data.setOutGoodsId(outGoodsId);
        data.setInCode(KitUtil.getInCode());
        data.setInType(2);
        data.setPurchaseCode(stockIn.getPurchaseCode());
        data.setPurchaseGoodsId(stockIn.getPurchaseGoodsId());
        data.setSupplierId(stockIn.getSupplierId());
        data.setSupplierName(stockIn.getSupplierName());
        data.setGoodsId(stockIn.getGoodsId());
        data.setGoodsName(stockIn.getGoodsName());
        data.setGoodsTypeId(stockIn.getGoodsTypeId());
        data.setGoodsTypeName(stockIn.getGoodsTypeName());
        data.setRealStatus(stockIn.getRealStatus());
        data.setUnit(stockIn.getUnit());
        data.setUnitPrice(stockIn.getUnitPrice());
        data.setSupplierPrice(stockIn.getSupplierPrice());
        data.setCreatorUid(headStaffUid);
        data.setCreator(headStaffName);
        data.setOperatorUid(headStaffUid);
        data.setOperator(headStaffName);
        data.setBuyNum(realNum);
        data.setRealNum(realNum);
        data.setUsableNum(realNum);
        data.setInAmount(DoubleUtil.mul(wetAvgPrice, realNum));
        data.setRemark("出库退回");
        data.setStatus(1);
        data.setStatusName("入库完成");
        data.setOkTime(time);
        return data;
    }

    /**
     * 新增出库商品使用的入库记录
     */
    public static WhStockOutGoodsLog newWhStockOutGoodsLog(
            ShopCart shopCart, String code, String outCode, WhStockIn stockIn, String headStaffUid, String headStaffName, Integer updateNum
    ) {
        WhStockOutGoodsLog log = new WhStockOutGoodsLog();
        log.setOrderCode(code);
        log.setOutCode(outCode);
        log.setShopCartId(shopCart.getId());
        log.setCreatorUid(headStaffUid);
        log.setCreator(headStaffName);
        log.setInCode(stockIn.getInCode());
        log.setPurchaseCode(stockIn.getPurchaseCode());
        log.setPurchaseGoodsId(stockIn.getPurchaseGoodsId());
        log.setSupplierId(stockIn.getSupplierId());
        log.setSupplierName(stockIn.getSupplierName());
        log.setGoodsId(stockIn.getGoodsId());
        log.setGoodsName(stockIn.getGoodsName());
        log.setGoodsTypeId(stockIn.getGoodsTypeId());
        log.setGoodsTypeName(stockIn.getGoodsTypeName());
        log.setRealStatus(stockIn.getRealStatus());
        log.setUnit(stockIn.getUnit());
        log.setNum(updateNum);
        log.setStatus(1);
        log.setStatusName("出库完成");

        log.setComboType(shopCart.getComboType());
        log.setMainComboType(shopCart.getMainComboType());
        log.setMainShopCartUid(shopCart.getMainShopCartUid());
        log.setMainGoodsId(shopCart.getMainGoodsId());
        log.setMainGoodsName(shopCart.getMainGoodsName());
        log.setMainGoodsTypeId(shopCart.getMainGoodsTypeId());
        log.setMainGoodsTypeName(shopCart.getMainGoodsTypeName());
        log.setWeight(shopCart.getWeight());
        log.setTotalWeight(DoubleUtil.mul(updateNum, shopCart.getWeight()));

        return log;
    }

    /**
     * 新增出库商品使用的入库记录
     */
    public static WhStockOutGoodsLog newWhStockOutGoodsLog(
            WhStockOutGoods stockOutGoods, String code, String outCode, WhStockIn stockIn, String headStaffUid, String headStaffName,
            Integer updateNum, Integer stockOutMethod, Long deliveryWarehouseId, String deliveryWarehouseName
    ) {
        WhStockOutGoodsLog log = new WhStockOutGoodsLog();
        log.setOrderCode(code);
        log.setOutCode(outCode);
        log.setShopCartId(stockOutGoods.getShopCartId());
        log.setCreatorUid(headStaffUid);
        log.setCreator(headStaffName);
        log.setLastStaffUid(headStaffUid);
        log.setLastStaffName(headStaffName);
        log.setInCode(stockIn.getInCode());
        log.setPurchaseCode(stockIn.getPurchaseCode());
        log.setPurchaseGoodsId(stockIn.getPurchaseGoodsId());
        log.setSupplierId(stockIn.getSupplierId());
        log.setSupplierName(stockIn.getSupplierName());
        log.setGoodsId(stockIn.getGoodsId());
        log.setGoodsName(stockIn.getGoodsName());
        log.setGoodsTypeId(stockIn.getGoodsTypeId());
        log.setGoodsTypeName(stockIn.getGoodsTypeName());
        log.setRealStatus(stockIn.getRealStatus());
        log.setUnit(stockIn.getUnit());
        log.setNum(updateNum);
        log.setStockOutMethod(stockOutMethod);
        log.setDeliveryWarehouseId(deliveryWarehouseId);
        log.setDeliveryWarehouseName(deliveryWarehouseName);
        log.setStatus(1);
        log.setStatusName("出库完成");

        log.setComboType(stockOutGoods.getComboType());
        log.setMainComboType(stockOutGoods.getMainComboType());
        log.setMainShopCartUid(stockOutGoods.getMainShopCartUid());
        log.setMainGoodsId(stockOutGoods.getMainGoodsId());
        log.setMainGoodsName(stockOutGoods.getMainGoodsName());
        log.setMainGoodsTypeId(stockOutGoods.getMainGoodsTypeId());
        log.setMainGoodsTypeName(stockOutGoods.getMainGoodsTypeName());
        log.setWeight(stockOutGoods.getWeight());
        log.setTotalWeight(DoubleUtil.mul(updateNum, stockOutGoods.getWeight()));

        return log;
    }

    /**
     * 创建加权平均价
     */
    public static GoodsWetPrice newGoodsWetPrice(Long goodsId, Long goodsTypeId, Integer usableNum, Double usablePrice) {
        double div = 0d;
        if (usablePrice != 0 && usableNum != 0) {
            div = DoubleUtil.div(usablePrice, usableNum, 4);
        }
        GoodsWetPrice newData = new GoodsWetPrice();
        newData.setGoodsId(goodsId);
        newData.setGoodsTypeId(goodsTypeId);
        newData.setUsableNum(usableNum);
        newData.setUsablePrice(usablePrice);
        newData.setWetAvePrice(div);
        return newData;
    }

    /**
     * 创建出库单
     */
    public static WhStockOut newWhStockOut(
            String headStaffUid, String headStaffName, String outCode, String orderCode, Integer outNum,
            Integer goodsTypeNum, Integer status, String statusName, String userName, String phone, String address,
            Integer stockOutMethod,
            String remark, Integer manualStatus
    ) throws Exception {
        WhStockOut data = new WhStockOut();
        data.setOutType(null);
        data.setOutCode(outCode);
        data.setOrderCode(orderCode);
        data.setCreatorUid(headStaffUid);
        data.setCreator(headStaffName);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        data.setGoodsTypeNum(goodsTypeNum);
        data.setNum(outNum);
        data.setName(userName);
        data.setPhone(phone);
        data.setAddress(address);
        data.setStatus(status);
        data.setStatusName(statusName);
        data.setStockOutMethod(stockOutMethod);
        data.setRemark(remark);
        data.setManualStatus(manualStatus);
        return data;
    }

    /**
     * 创建出库商品记录
     */
    public static WhStockOutGoods newWhStockOutGoods(
            String headStaffUid, String headStaffName, ShopCart shopCart, String outCode, String orderCode,
            Integer outNum, Integer status, String statusName, double wetAvgPrice
    ) {
        WhStockOutGoods data = new WhStockOutGoods();
        data.setOutCode(outCode);
        data.setOrderCode(orderCode);
        data.setCreatorUid(headStaffUid);
        data.setCreator(headStaffName);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        data.setStatus(status);
        data.setStatusName(statusName);
        data.setWetAvgPrice(wetAvgPrice);
        data.setShopCartId(shopCart.getId());
        data.setGoodsId(shopCart.getGoodsId());
        data.setGoodsName(shopCart.getGoodsName());
        data.setGoodsTypeId(shopCart.getGoodsTypeId());
        data.setGoodsTypeName(shopCart.getGoodsTypeName());
        data.setUnit(shopCart.getUnit());
        data.setRealStatus(shopCart.getRealStatus());
        data.setPrice(shopCart.getPrice());
        data.setUnitPrice(shopCart.getUnitPrice());
        data.setBuyNum(outNum);
        data.setNum(outNum);

        data.setComboType(shopCart.getComboType());
        data.setMainComboType(shopCart.getMainComboType());
        data.setMainShopCartUid(shopCart.getMainShopCartUid());
        data.setMainGoodsId(shopCart.getMainGoodsId());
        data.setMainGoodsName(shopCart.getMainGoodsName());
        data.setMainGoodsTypeId(shopCart.getMainGoodsTypeId());
        data.setMainGoodsTypeName(shopCart.getMainGoodsTypeName());
        data.setWeight(shopCart.getWeight());
        data.setTotalWeight(DoubleUtil.mul(outNum, shopCart.getWeight()));

        data.setFreeDeliveryId(shopCart.getFreeDeliveryId());
        data.setFreeDeliveryName(shopCart.getFreeDeliveryName());
        data.setFreeDeliveryStatus(shopCart.getFreeDeliveryStatus());

        return data;
    }

    /**
     * 创建出库单的发货记录
     */
    public static WhStockOutDelivery newStockOutDelivery(
            WhStockOutDelivery data, String headStaffUid, String headStaffName, Integer type, String outCode, String orderCode,
            Integer stockOutMethod, String name, String phone, String address,
            String expressName, String expressCode, Double expressPrice, Integer expressPayType, String expressTime,
            Integer packageNum, Long deliveryWarehouseId, String deliveryWarehouseName, String remark
    ) throws Exception {
        if (data == null) {
            data = new WhStockOutDelivery();
            data.setType(type);
            data.setOutCode(outCode);
            data.setOrderCode(orderCode);
            data.setAddStaffUid(headStaffUid);
            data.setAddStaffName(headStaffName);
        }
        data.setName(name);
        data.setPhone(phone);
        data.setAddress(address);
        data.setExpressName(expressName);
        data.setExpressCode(expressCode);
        data.setExpressPrice(expressPrice);
        data.setExpressPayType(expressPayType);
        data.setPackageNum(packageNum);
        if (StringUtils.isNotBlank(expressTime))
            data.setExpressTime(DateUtil.parseDateFromStr(expressTime));
        data.setStockOutMethod(stockOutMethod);
        if (stockOutMethod.equals(1)) {
            data.setDeliveryWarehouseId(1L);
            data.setDeliveryWarehouseName("自营仓库");
        } else if (stockOutMethod.equals(2)) {
            data.setDeliveryWarehouseId(0L);
            data.setDeliveryWarehouseName("线上发货");
        } else {
            if (null != deliveryWarehouseId)
                data.setDeliveryWarehouseId(deliveryWarehouseId);
            if (null != deliveryWarehouseName)
                data.setDeliveryWarehouseName(deliveryWarehouseName);
        }
        data.setRemark(remark);
        data.setLastStaffUid(headStaffUid);
        data.setLastStaffName(headStaffName);
        return data;
    }

    /**
     * 设置货记录信息
     */
    public static StockOutDeliveryDto getDeliveryObj(WhStockOutDelivery delivery, List<WhStockOutDeliveryImg> imgList) {
        StockOutDeliveryDto data = new StockOutDeliveryDto();
        data.setId(delivery.getId());
        data.setOutCode(delivery.getOutCode());
        data.setExpressName(delivery.getExpressName());
        data.setExpressCode(delivery.getExpressCode());
        data.setExpressPrice(delivery.getExpressPrice());
        data.setExpressPayType(delivery.getExpressPayType());
        data.setExpressTime(delivery.getExpressTime());
        data.setPackageNum(delivery.getPackageNum());
        data.setStockOutMethod(delivery.getStockOutMethod());
        data.setDeliveryWarehouseId(delivery.getDeliveryWarehouseId());
        data.setDeliveryWarehouseName(delivery.getDeliveryWarehouseName());
        data.setStaffName(delivery.getLastStaffName());
        data.setImgList(imgList);
        data.setRemark(delivery.getRemark());
        data.setStatus(delivery.getType());
        return data;
    }

}
